<template>
  <div id="loading-box" ref="loading-box">
    <div class="loading-left-bg"></div>
    <div class="loading-right-bg"></div>
    <div class="spinner-box" ref="spinner-box">
      <div class="configure-border-1">
        <div class="configure-core"></div>
      </div>
      <div class="configure-border-2">
        <div class="configure-core"></div>
      </div>
      <div class="loading-word">正在准备中</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Loading',
  data() {
    return {}
  },
  methods: {
    // 结束加载
    endLoading() {
      // document.body.style.overflow = 'auto';
      this.$refs['loading-box'].classList.add("loaded")
      this.$refs['spinner-box'].style.display = 'none'
    }
  },
  mounted() {
    // 当页面加载完成隐藏全部加载动画
    window.addEventListener('load', () => {
      this.endLoading()
    })
    // 防止关闭加载失效
    setTimeout(() => {
      this.endLoading();
    }, 2000);
  },
  destroyed() {
    // // 当页面加载完成隐藏全部加载动画
    // window.removeEventListener('load', () => {
    //   this.endLoading()
    // })
  }
}
</script>

<style lang="less" scoped>
#loading-box.loaded {
  z-index: 5000;
}

/*左右打开的样式*/
.loading-bg, #loading-box .loading-left-bg, #loading-box .loading-right-bg {
  position: fixed;
  z-index: 1000;
  width: 50%;
  height: 100%;
  background: #37474f;
}


#loading-box.loaded .loading-right-bg {
  transition: all 1s;
  transform: translate(100%, 0);
}

#loading-box .loading-right-bg {
  right: 0;
}

#loading-box .loading-left-bg {
  left: 0;
}

#loading-box.loaded .loading-left-bg {
  transition: all 1s;
  transform: translate(-100%, 0);
}

#loading-box .spinner-box {
  position: fixed;
  z-index: 1001;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
}

/*中间的动画*/
#loading-box .spinner-box .configure-border-1 {
  position: absolute;
  padding: 3px;
  width: 115px;
  height: 115px;
  background: #ffab91;
  animation: configure-clockwise 3s ease-in-out 0s infinite alternate;
}

#loading-box .spinner-box .configure-core {
  width: 100%;
  height: 100%;
  background-color: #37474f;
}

@keyframes configure-clockwise {
  0% {
    -webkit-transform: rotate(0);
    -moz-transform: rotate(0);
    -o-transform: rotate(0);
    -ms-transform: rotate(0);
    transform: rotate(0);
  }
  25% {
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg);
  }
  50% {
    -webkit-transform: rotate(180deg);
    -moz-transform: rotate(180deg);
    -o-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    transform: rotate(180deg);
  }
  75% {
    -webkit-transform: rotate(270deg);
    -moz-transform: rotate(270deg);
    -o-transform: rotate(270deg);
    -ms-transform: rotate(270deg);
    transform: rotate(270deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    -ms-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

#loading-box .spinner-box .configure-border-2 {
  left: -115px;
  padding: 3px;
  width: 115px;
  height: 115px;
  background: #3ff9dc;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  animation: configure-xclockwise 3s ease-in-out 0s infinite alternate;
}

@keyframes configure-xclockwise {
  0% {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
  }
  25% {
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    transform: rotate(-45deg);
  }
  50% {
    -webkit-transform: rotate(-135deg);
    -moz-transform: rotate(-135deg);
    -o-transform: rotate(-135deg);
    -ms-transform: rotate(-135deg);
    transform: rotate(-135deg);
  }
  75% {
    -webkit-transform: rotate(-225deg);
    -moz-transform: rotate(-225deg);
    -o-transform: rotate(-225deg);
    -ms-transform: rotate(-225deg);
    transform: rotate(-225deg);
  }
  100% {
    -webkit-transform: rotate(-315deg);
    -moz-transform: rotate(-315deg);
    -o-transform: rotate(-315deg);
    -ms-transform: rotate(-315deg);
    transform: rotate(-315deg);
  }
}

/*显示的loading文字样式*/
#loading-box .spinner-box .loading-word {
  position: absolute;
  color: #fff;
  font-size: 20px;
}
</style>
